package com.example.object.screen;

import android.content.Context;
import android.util.Log;

import com.example.bounce.R;
import com.example.globject.GLSquare;
import com.example.object.GameShape;
import com.example.shape.Square;
import com.example.state.AppState;

import org.jbox2d.common.Vec2;

/**
 * Created by Lucas on 26/06/13.
 */
public class Locker extends GameShape {

    /**
     * The square to calculate if the object is touched
     */
    private Square _square;
    /**
     * The Gl Object of the locker
     */
    private GLSquare _glSquare;
    /**
     * The debug TAG
     */
    private final String TAG = "LockerShape";
    /**
     * Define is the locker is open or not
     */
    private boolean _isOpen;

    /**
     * The constructor
     * @param context the context of the application
     * @param xCenter the x origin coordinate of the locker
     * @param yCenter the y origin coordinate of the locker
     * @param isOpen the boolean that defines if the locker is open or not
     * @param ball the ball inside the locker
     */
    public Locker(Context context, float xCenter, float yCenter, boolean isOpen,int ball){
        _handlerInitiate = false;
        Log.i(TAG, "Locker constructor");
        _userData = ball;
        _isOpen = isOpen;
        _square = new Square(yCenter,xCenter, 0.25f*AppState.SIMULATION_HEIGHT, (1f/6f)*AppState.SIMULATION_WIDTH);
        if (isOpen){
            _glSquare = new GLSquare(context, xCenter, yCenter, 0.25f*AppState.SIMULATION_HEIGHT, (1f/6f)*AppState.SIMULATION_WIDTH, R.raw.lockeropen);
        }
        else {
            _glSquare = new GLSquare(context, xCenter, yCenter, 0.25f*AppState.SIMULATION_HEIGHT, (1f/6f)*AppState.SIMULATION_WIDTH, R.raw.locker);
        }
    }

    /**
     * return a boolean to know if the locker is open and so touchable
     * @return _isOpen
     */
    @Override
    public boolean isTouchable() {
        return _isOpen;
    }

    /**
     * Set the matrix and Id of the opengl resources
     * @param positionHandle
     * @param textureHandle
     * @param MVPMatrixHandle
     * @param vMatrix
     * @param projMatrix
     */
    public void setGLHandler(int positionHandle, int textureHandle, int MVPMatrixHandle,
                             float vMatrix[], float projMatrix[]){
        _glSquare.setGLHandler(positionHandle,textureHandle,MVPMatrixHandle,vMatrix,projMatrix, null);
        _handlerInitiate = true;
    }

    /**
     * Draw the locker
     */
    @Override
    public void draw(){
        _glSquare.draw();
    }

    /**
     * return is the locker is touched
     * @param touch the input point
     * @return true if the locker is touched
     */
    public boolean isTouched(Vec2 touch){
        if(_square.isTouched(touch)){
            Log.i(TAG, " Locker is touched.");
            return true;
        }
        return false;
    }

    /**
     * return is the locker is touched
     * @param x the X input point
     * @param y the Y input point
     * @return true if the locker is touched
     */
    public boolean isTouched(float x, float y){
        if(_square.isTouched(x, y)){
            Log.i(TAG, " Locker is touched.");
            return true;
        }
        //Log.i(TAG, _text + " is not touched with x:y + "+x+":"+y + " and "+toString());
        return false;
    }

    public String toString(){
        return "glSquare: " + _glSquare.toString() + "\n square :" + _square.toString();
    }

    public Square getSquare(){
        return _square;
    }

    public GLSquare getGLSquare(){
        return _glSquare;
    }
}
